<script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.min.js" charset="UTF-8"></script>
 <script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.easyui.min.js" charset="UTF-8"></script>
 <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/default/easyui.css" type="text/css"></link>
 <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/icon.css" type="text/css"></link>
 <link rel="stylesheet" href="../css/main.css" type="text/css"></link>
 <script type="text/javascript" src="../js/mine.js"></script>
 <script type="text/javascript" src="../js/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js" charset="UTF-8"></script>
	 
<script type="text/javascript" charset="UTF-8">
	var teacherProjectDatagrid;
	var teacherProjectLayout;
	var teacherProjectForm;
	var editRow;
	var projectDetailDialog;
	$(function(){
		teacherProjectForm = $('#teacherProjectForm').form();
		teacherProjectLayout = $('#teacherProjectLayout').layout();
		editRow = undefined;


		teacherProjectDatagrid = $('#teacherProjectDatagrid').datagrid({
			url:'../servlet/GetTOProjectServlet',
			//iconCls:'icon-save',
			fitColumns:true,
			nowrap:false,
			idField:'pno',
			//rownumbers:true,
			pagination:true,
			pageSize:10,
			pageList:[10,20,30],
			fit:true,
			border:false,
			sortName:'pno',
			sortOrder:'desc',
			columns:[[ {
				title:'项目编号',
				field:'pno',
				width:100,
				sortable:true,
				checkbox:true,
			},{
				title:'项目名称',
				field:'pname',
				width:80,
				sortable:true,
				formatter: function(value,rowData,rowIndex){
		            return '<a href="javascript:void(0);" onclick="showProjectDetail(' + rowIndex + ');" style="text-decoration: none;">'+value+'</a>';
		     	}  	
			},{
				title:'创建时间',
				field:'stime',
				width:45,
				sortable:true
			},{
				title:'截止时间',
				field:'etime',
				width:45,
				sortable:true
			},{
				title:'发布者',
				field:'pubname',
				width:30
			},{
				title:'项目状态',
				field:'progress',
				sortable:true,
				width:30,
				formatter: function(value,rowData,rowIndex){
					if(value == '1') {
						return '已完成';
					} else {
						return '进行中';
					}
		     	} 
			},{
				title:'讨论区',
				field:'discuss',
				width:30,
				formatter: function(value,rowData,rowIndex){
		            return '<a href="javascript:void(0);" onclick="showDiscuss(' + rowIndex + ');">'+ value +'</a>';
		     	}  
			},{
				title:'成果区',
				field:'show',
				width:30,
				formatter: function(value,rowData,rowIndex){
					 return '<a href="javascript:void(0);" onclick="showAchieve(' + rowIndex + ');">' + value +'</a>';
		     	}
			},{
				title:'更改进度',
				field:'change',
				width:30,
				formatter: function(value,rowData,rowIndex){
					var action = -1;
					var s = "";
					if(rowData.progress == '1') {//状态为已完成
						action = 0;
						s = "未完成";
					} else {
						action = 1;
						s = "完成";
					} 
					 	
					return '<a href="javascript:void(0);" onclick="changeProgress(' + action + ', ' + rowData.pno + ');">' + s + '</a>';
				} 
			}]],
			toolbar:[{
				text:'增加',
				iconCls:'icon-add',
				handler:function(){
					addProject();
				}
			},'-',{
				text:'删除',
				iconCls:'icon-cancel',
				handler:function(){
					delProject();
				}
			},'-',{
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){
					modifyProject();
				}
			},'-',{
				text:'取消',
				iconCls:'icon-undo',
				handler:function(){
					teacherProjectDatagrid.datagrid('unselectAll');
					teacherProjectDatagrid.datagrid('rejectChanges');
				}
			},'-',{
				text:'刷新',
				iconCls:'icon-reload',
				handler:function(){
					teacherProjectDatagrid.datagrid('unselectAll');
					teacherProjectDatagrid.datagrid('rejectChanges');
					teacherProjectDatagrid.datagrid('load');
				}
			},'-']
		});
		
		$('#progress').combobox({
			data:[{
				"id":"all",
				"text":"全部",
			},{
				"id":"0",
				"text":"进行中"
			},{
				"id":"1",
				"text":"已完成",
			}],
			valueField:'id',
			textField:'text',
			editable:false
		});
	});
	delProject = function() {
		var rows = teacherProjectDatagrid.datagrid('getSelections');
		var ids = [];
		var cantd = [];
		var m = '';
		var cantm = '';
		
		console.info(rows.length);
		if(rows.length > 0) {
			for(var i=0;i<rows.length; i++) {
				if(rows[i].progress == '1') {
					ids.push(rows[i].pno);
				} else {
					cantd.push(rows[i].pno);
					cantm += "<br/> [" + rows[i].pname + "]";
				}
			}
			
			if(cantd.length > 0 && ids.length == 0) {//选择的数据都不能删除
				m = '选择的数据都不能删除.';
				$.messager.alert('提示',m,'warning');
			} else {
				if(cantd.length > 0 && ids.length > 0){//选择的数据部分不能删除
					m = '<div style="width:10px;margin:auto auto auto 45px;">选择的项目中:' + cantm + '<br/>还未完成,不能删除.<br>确定要删除选中的其余数据吗?</div>';
				} else if(cantd.length == 0 && ids.length > 0) {//选择的数据都可以删除
					m = '确定要删除所选择的项目吗?';
				}
			
				$.messager.confirm('提示',m,function(b) {
					if(b) {
						$.ajax({
							url:'../servlet/DeleteProjectServlet',
							data:{
								ids:ids.join(',')//用,分隔
							},
							type:'post',
							dataType:'json',
							success:function(r) {
								if(r && r.success) {
									teacherProjectDatagrid.datagrid('acceptChanges');
									teacherProjectDatagrid.datagrid('load');
									teacherProjectDatagrid.datagrid('unselectAll');
									teacherProjectDatagrid.datagrid('rejectChanges');
									//console.info(teacherProjectDatagrid.datagrid('getSelections'));
									$.messager.show({
										title:'提示',
										msg:r.msg
									});
								} else {
									$.messager.alert('提示',r.msg,'error');
								}
							}
						});
					}
				});
			}
		} else {
			$.messager.alert('提示','未选择任何数据.','warning');
		} 
	}
	
	changeProgress = function(action, pno) {
		if(action != null && action != -1) {
			var msg;
			if(action == 1) {
				msg = '确定将项目进度改成已完成吗?';
			} else {
				msg = '确定将项目进度改成进行中吗?';
			}
			$.messager.confirm('提示',msg,function(b) {
				if(b) {
					$.ajax({
						//url:'',
						url:'../servlet/ChangeProgressServlet',
						data:{
							action:action,
							pno:pno
						},
						type:'post',
						dataType:'json',
						success:function(r) {
							if(r && r.success) {
								teacherProjectDatagrid.datagrid('acceptChanges');
								teacherProjectDatagrid.datagrid('load');
								teacherProjectDatagrid.datagrid('unselectAll');
								teacherProjectDatagrid.datagrid('rejectChanges');
								//console.info(teacherProjectDatagrid.datagrid('getSelections'));
								$.messager.show({
									title:'提示',
									msg:r.msg
								});
							} else {
								$.messager.alert('出错啦',r.msg,'error');
							}
						}
					});
				}
		});
		} else {
			$.messager.alert('出错啦','改变进度失败.','error');
		}
	}
	
	showDiscuss = function(i) {
		var rows = teacherProjectDatagrid.datagrid('getRows');
		console.info(rows[i]);
		
		var menuTab = parent.$('#menuTab', parent.document).tabs();//得到tabs对象
		var tabText = '讨论区_' + rows[i].pname;
		var tabUrl = 'teacher/discuss.jsp?pno=' + rows[i].pno;
		//console.info(menuTab);
		if(menuTab.tabs('exists', tabText)) {
			menuTab.tabs('select', tabText);
		} else {
			menuTab.tabs('add',{
				title: tabText,
				closable:true,
				//content:tabText
				content:'<iframe src="'+ tabUrl +'" frameborder="0" style="border:0;width:100%;height:100%;"></iframe>'
			});
		}
		
	}
	
	showAchieve = function(i) {
		var rows = teacherProjectDatagrid.datagrid('getRows');
		
		var menuTab = parent.$('#menuTab', parent.document).tabs();//得到tabs对象
		var tabText = '成果区_' + rows[i].pname;
		var tabUrl = 'teacher/achivement.jsp?pno=' + rows[i].pno;
		//console.info(menuTab);
		if(menuTab.tabs('exists', tabText)) {
			menuTab.tabs('select', tabText);
		} else {
			menuTab.tabs('add',{
				title: tabText,
				closable:true,
				//content:tabText
				content:'<iframe src="'+ tabUrl +'" frameborder="0" style="border:0;width:100%;height:100%;"></iframe>'
			});
		}
		
	}
	
	addProject = function() {
		addProjectDialog = parent.$('#addProjectDialog', parent.document).dialog({
			title:'发布项目',
			modal:true,
			method:'post',
			collapsible:false,
			showHeader:false,
			width:700,
			height:550,
			href:'teacher/addProject.html'
		});	
		
	}
	
	modifyProject = function() {
		var rows = teacherProjectDatagrid.datagrid('getSelections');
		if(rows.length == 1 ) {
			var title = '修改项目_' + rows[0].pname;
			console.info(title);
			modifyProjectDialog = parent.$('#modifyProjectDialog', parent.document).dialog({
				title:title,
				modal:true,
				collapsible:false,
				showHeader:false,
				width:700,
				height:550,
				href:'teacher/modifyProject.jsp?pno=' + rows[0].pno
			});
			
			modifyProjectDialog.dialog('open');
		} else if(rows.length > 1) {
			$.messager.alert('提示','只能选择一行数据进行修改.','warning');
		} else {
			$.messager.alert('提示','未选择需要修改的数据.', 'warning');
		}
	}

	
	showProjectDetail = function(i) {
		var rows = teacherProjectDatagrid.datagrid('getRows');
		var title = '项目详情_' + rows[i].pname;
		console.info(title);
		projectDetailDialog = parent.$('#projectDetailDialog', parent.document).dialog({
			title:title,
			modal:true,
			collapsible:false,
			showHeader:false,
			width:650,
			height:550,
			href:'teacher/projectDetail.jsp?pno=' + rows[i].pno + '&pname=' + rows[i].pname
		});
		
		projectDetailDialog.dialog('open');
	}
	
	searchProject = function() {
		console.info(serializeObject(teacherProjectForm));
		teacherProjectDatagrid.datagrid('load',serializeObject(teacherProjectForm));
	}

	cleanProject = function() {
		teacherProjectDatagrid.datagrid('load',{});
		teacherProjectForm.find('input').val('');
	}
</script>

<div id="teacherProjectLayout" fit="true" border="false" >
			<div region="north" border="false" split="false" title="查询" style="margin:0 auto;height:81px;" >
				<form id="teacherProjectForm"  class="datagrid-toolbar" style="margin:0 auto;">
				  <table>
					<tr>
						<td><b>项目名称</b><input type="text" name="pname" id="pname"/></td>
						<td ><b>创建时间</b><input type="text" name="stimef" id="stimef" class="easyui-datetimebox" />-</td>
						<td><input type="text" name="stimet" id="stimet" class="easyui-datetimebox" /></td>
					</tr>
					<tr>
						<td><b>项目进度</b><input type="text" name="progress" id="progress"/></td>
						<td><b>截止时间</b><input type="text" name="etimef" id="etimef" class="easyui-datetimebox" />-</td>
						<td><input type="text" name="etimet" id="etimet" class="easyui-datetimebox" /></td>
						<td>
							<a href="javascript:void(0);" class="easyui-linkbutton"  iconCls="icon-search" pain="false" onclick="searchProject();" ></a>
						</td>
						<td>
							<a href="javascript:void(0);" class="easyui-linkbutton"  pain="false" onclick="cleanProject();" >清空</a>
						</td>
					</tr>
				 </table>
			  </form>
		  </div>
	<div region="center" border="false" >
		<table id="teacherProjectDatagrid"></table>
	</div>
</div>

	
